#pragma once
#include <iostream>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Log.hpp"
#include <cstring>
#include "InetAddr.hpp"
#include <sys/wait.h>
#include <functional>
#include <memory>
using namespace std;
const static uint16_t gport = 8888;
const static int gsock = -1;
const static int gblcklog = 8;
enum
{
    SOCKET_ERROR = 1,
    BIND_ERROR,
    LISTEN_ERROR
};
class Socket;
using SockSPtr = std::shared_ptr<Socket>;

class Socket
{
public:
    virtual void CreateSocketOrDie() = 0;
    virtual void CreateBindOrDie(uint16_t port = gport) = 0;
    virtual void CreateListenOrDie(int backlog = gblcklog) = 0;
    virtual int Accepter(InetAddr *cliaddr) = 0;
    virtual bool Connector(const string &serverip, uint16_t serverport) = 0;
    virtual int sockfd() = 0;
    virtual int Close() = 0;
    virtual int Recv(string *out) = 0;
    virtual int Send(string &in) = 0;

public:
    bool BuildListenSocket(uint16_t port = gport)
    {
        CreateSocketOrDie();
        CreateBindOrDie(port);
        CreateListenOrDie();
        return true;
    }
    bool BuildClientSocket(const string &serverip, uint16_t serverport = gport)
    {
        CreateSocketOrDie();
        return Connector(serverip, serverport);
    }
};
class TcpSocket : public Socket
{
public:
    TcpSocket(int sockfd = gsock) : _sockfd(sockfd) {}
    void CreateSocketOrDie()
    {
        _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
        if (_sockfd < 0)
        {
            LOG(FATAL, "socket create error\n");
            exit(SOCKET_ERROR);
        }
        LOG(INFO, "socket create success\n");
    }
    void CreateBindOrDie(uint16_t port = gport)
    {
        _port = port;
        struct sockaddr_in local;
        local.sin_family = AF_INET;
        local.sin_port = htons(_port);
        local.sin_addr.s_addr = INADDR_ANY;
        int n = ::bind(_sockfd, (sockaddr *)&local, sizeof(local));
        if (n < 0)
        {
            LOG(FATAL, "bind error\n");
            exit(BIND_ERROR);
        }
        LOG(INFO, "bind success\n");
    }
    void CreateListenOrDie(int backlog = gblcklog)
    {
        if (::listen(_sockfd, backlog) < 0)
        {
            LOG(FATAL, "listen error\n");
            exit(LISTEN_ERROR);
        }
        LOG(INFO, "listen success\n");
    }
    int Accepter(InetAddr *cliaddr)
    {

        struct sockaddr_in client;
        socklen_t len = sizeof(client);
        int sockfd = ::accept(_sockfd, (struct sockaddr *)&client, &len);
        if (sockfd < 0)
        {
            LOG(WARNING, "accept error\n");
            return -1;
        }
        *cliaddr = InetAddr(client);
        LOG(INFO, "new connect %s\n", cliaddr->Ip().c_str());
        return sockfd;
    }
    bool Connector(const string &serverip, uint16_t serverport)
    {
        struct sockaddr_in server;
        server.sin_port = serverport;
        server.sin_family = AF_INET;
        ::inet_pton(AF_INET, serverip.c_str(), &server.sin_addr);
        int n = ::connect(_sockfd, (struct sockaddr *)&server, sizeof(server));
        if (n < 0)
        {
            cerr << "connect error" << endl;
            return false;
        }
        return true;
    }
    int sockfd()
    {
        return _sockfd;
    }
    int Close()
    {
        if (_sockfd > 0)
            ::close(_sockfd);
        return 1;
    }
    int Recv(string *out)
    {
        char buf[2048];
        ssize_t n = ::recv(_sockfd, buf, sizeof(buf) - 1, 0);
        if (n > 0)
        {
            buf[n] = 0;
            *out += buf;
        }
        return n;
    }
    int Send(string &in)
    {
        return ::send(_sockfd, in.c_str(), in.size(), 0);
    }

private:
    uint16_t _port;
    int _sockfd; //_listensockfd or _sockfd;
};